<template>
  <j-modal
    :title="title"
    :width="1000"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
       <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type" label="用户类型">
		   <a-select v-model="model.type" placeholder="请选择用户类型">
		     <a-select-option value="1">宝妈</a-select-option>
		     <a-select-option value="2">月嫂</a-select-option>
		   </a-select>
		</a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bannerUrl" label="背景图">
           <a-upload
               name="file"
			   :headers="headers"
               list-type="picture-card"
               class="avatar-uploader"
               :show-upload-list="false"
               action="https://www.shoujiakang.cn/api/sys/common/upload"
               :before-upload="beforeUpload"
               @change="handleChange"
			   @preview="handlePreview"
             >
               <img v-if="model.bannerUrl" :src="model.bannerUrl" alt="avatar" style="width:694px;height:320px;object-fit: cover;" />
               <div v-else>
                 <a-icon :type="loading ? 'loading' : 'plus'" />
                 <div class="ant-upload-text">
                   背景图
                 </div>
               </div>
             </a-upload>
			 <a-modal :visible="previewVisible" :footer="null" @cancel="handleCancel">
			       <img alt="example" style="width: 100%" :src="previewImage" />
			     </a-modal>
        </a-form-model-item>
		
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import moment from "moment"
  import Vue from 'vue'
  import { ACCESS_TOKEN } from "@/store/mutation-types"
  export default {
    name: "WechatBannerModal",
    data () {
      return {
        title:"操作",
        visible: false,
        model: {
			type: '1'
		},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules:{
        },
		loading: false,
		imageUrl: '',
		token: {},
		previewImage: '',
		previewVisible: false,
        url: {
          add: "/wechat/wechatBanner/add",
          edit: "/wechat/wechatBanner/edit",
        },
      }
    },
    created () {
		const token = Vue.ls.get(ACCESS_TOKEN);
		this.headers = {"X-Access-Token":token}
    },
    methods: {
		async handlePreview(file) {
		      if (!file.url && !file.preview) {
		        file.preview = await getBase64(file.originFileObj);
		      }
		      this.previewImage = file.url || file.preview;
		      this.previewVisible = true;
		    },
		handleChange(info) {
		      if (info.file.status === 'uploading') {
		        this.loading = true;
		        return;
		      }
		      if (info.file.status === 'done') {
		        // Get this url from response in real world.
		        this.model.bannerUrl = info.file.response.message
		        this.loading = false;
		      }
		    },
	  beforeUpload(file) {
	        const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
	        if (!isJpgOrPng) {
	          this.$message.error('You can only upload JPG file!');
	        }
	        const isLt2M = file.size / 1024 / 1024 < 2;
	        if (!isLt2M) {
	          this.$message.error('Image must smaller than 2MB!');
	        }
	        return isJpgOrPng && isLt2M;
	      },
      add () {
        //初始化默认值
        this.edit({type : '1'});
      },
      edit (record) {
        this.model = Object.assign({}, record);
		this.model.type = this.model.type ? this.model.type+'' : '1'
        this.visible = true;
      },
      close () {
        this.$emit('close');
        this.visible = false;
        this.$refs.form.clearValidate();
      },
      handleOk () {
        const that = this;
        // 触发表单验证
         this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }else{
             return false;
          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>
.avatar-uploader > .ant-upload {
  width: 128px;
  height: 128px;
}
.ant-upload-select-picture-card i {
  font-size: 32px;
  color: #999;
}

.ant-upload-select-picture-card .ant-upload-text {
  margin-top: 8px;
  color: #666;
}
/deep/ .ant-upload.ant-upload-select-picture-card {
	width:694px;
	height:320px;
}
</style>